package com.redis.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author zqf
 * @Date 2022/11/2 9:25
 * @Description: 监听key失效机制
 */

@RestController("/keyExpired")
@Api(tags = "redis监听key失效机制，可以用来做简单得定时任务")
public class KeyExpiredController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Value("${redis.expiredKey}")
    private String expiredKey;


    @GetMapping("/setExpiredKey")
    @ApiOperation("设置过期key")
    private String testSetExpiredKey(@RequestParam("expiredTime") @ApiParam("过期时间，单位秒") Integer expiredTime){
        redisTemplate.opsForValue().set(expiredKey,"过期得key",expiredTime * 1000, TimeUnit.MILLISECONDS);
        return "设置成功";
    }
}
